//
//  LHNetWorkTools.swift
//  sina_weibo
//
//  Created by 刘刘海强 on 16/5/12.
//  Copyright © 2016年 chuanzhi. All rights reserved.
//

import UIKit
import AFNetworking

enum RequestType: Int {
    case GET = 0
    case POST = 1
}

class LHNetWorkTools: AFHTTPSessionManager {
    
    //设置单例
    static let sharedTools: LHNetWorkTools = {
        let tools = LHNetWorkTools()
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools
    }()

    
    //设置网络请求，仿照GET/SET方法自定义的数据请求方法
    func request(requestType: RequestType, URLString: String, parameters: AnyObject?, callBack: (response: AnyObject? ,error: NSError?)->() ) {
    
        //定义成功的闭包,其中参数的可选必选类型是参照post/get方法的参数类型确定的
        let successClosure = {(dataTask: NSURLSessionDataTask, response: AnyObject?) -> Void in
            callBack(response: response, error: nil)
        }
        
        let failureClosure = {(dataTask: NSURLSessionDataTask?,error: NSError) -> Void in
            callBack(response: nil, error: error)
        }
        
        if requestType == .GET {
            GET(URLString, parameters: parameters, progress: nil, success: successClosure, failure: failureClosure)
        }else{
            POST(URLString, parameters: parameters, progress: nil, success: successClosure, failure: failureClosure)
        }
        
    
    }

}


//发送微博接口
extension LHNetWorkTools {
    //发送文字微博
    func update(accessToken: String, status: String, callBack: (response: AnyObject?, error: NSError?)->()) {
        
        let url = "https://api.weibo.com/2/statuses/update.json"
        let parameters = [
            "access_token":accessToken,
            "status":status
        ]
        request(.POST, URLString: url, parameters: parameters, callBack: callBack)
    }
    
    
}

//首页接口
extension LHNetWorkTools {
    func requestStatus(accessToken: String, maxId: Int64 = 0, sinceId: Int64 = 0, callBack: (response: AnyObject?, error: NSError?)->()) {

        let url = "https://api.weibo.com/2/statuses/friends_timeline.json"
        let parameters = [
            "access_token":accessToken,
            "max_id":"\(maxId)",
            "since_id":"\(sinceId)"
        ]
        print(url + "?access_token=\(accessToken)")
        
        request(.GET, URLString: url, parameters: parameters, callBack: callBack)
    }
}


//OAuth相关接口
extension LHNetWorkTools {
    //获取accessToken的接口
    func requestAccessToken(code: String, callBack: (response: AnyObject?, error: NSError?)->()) {
        let url = "https://api.weibo.com/oauth2/access_token"
        let parameters = [
            "client_id":WeiboAppKey,
            "client_secret":weiboAppSecret,
            "grant_type":"authorization_code",
            "code":code,
            "redirect_uri":WeiboRedirect_Uri
        ]
        request(.POST, URLString: url, parameters: parameters, callBack: callBack)
    }
    
    //获取用户数据的接口
    func requestUserInfo(userAccount: LHUserAccount, callBack: (response: AnyObject?, error: NSError?)->()) {
        let url = "https://api.weibo.com/2/users/show.json"
        let parameters = [
            "access_token":userAccount.access_token!,
            "uid":"\(userAccount.uid)"
        ]
        request(.GET, URLString: url, parameters: parameters, callBack: callBack)
    }
}
